﻿using NfsBLL;
using NfsCommon.Util;
using NfsModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;

namespace ManagerWed.Controllers
{
    public class SqauthorizationController : Controller
    {
        //
        // GET: /Sqauthorization/
        public ActionResult Index()
        {
            ViewData["hiden_type"] = CookieHelper.GetDecodeCookie("pcnfs_type");
            return View();
        }
        #region 申请更新查询
        public JsonResult query_Sqauthorization(int pageindex, int pagesize, string Name, string mobile, string level_id, string state)
        {
            try
            {
                Sq_authorizationBLL bll = new Sq_authorizationBLL();
                List<sq_authorization> list = bll.GetSqAuBycondtion(pageindex, pagesize, Name, mobile, level_id, state);
                object pagecount = bll.GetSqAuBycondtionCount(Name, mobile, level_id, state);
                return Json(new { MessageCode = "0", Shouquanlist = list, Pagecount = (int.Parse(pagecount.ToString()) + pagesize - 1) / pagesize }, JsonRequestBehavior.AllowGet);

            }
            catch (Exception)
            {

                return Json(new { MessageCode = "200"}, JsonRequestBehavior.AllowGet);

            }
       }
        #endregion
        public JsonResult GetAgency(int agency_id, int auid)
        {
             
            
                AgencyBLL bll = new AgencyBLL();
                agency model = bll.GetModel2(agency_id, auid);
                if (model != null)
                {
                    model.start_time = model.austart_time;
                    model.end_time = model.auend_time;
                    account act = new AccountBLL().GetModel((int)model.account_id);
                    return Json(new { MessageCode = "0", model = model, pwd = act != null ? act.user_pwd : "" }, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);

                }

        }
     
        #region  授权
        [HttpPost]
        public JsonResult Authorization(int id, int state)
        {
            try
            {
                Sq_authorizationBLL bll = new Sq_authorizationBLL();
                int agency_id;
                bool flag = bll.updateShouQuan(id, state, out agency_id);
                string au_code = string.Empty;

                if (flag == true)
                {
                        
                            AgencyBLL agbll = new AgencyBLL();
                            agency agmodel = agbll.GetModel(agency_id);
                            if (state == 1) { }
                            AuthorizationBLL aubll = new AuthorizationBLL();
                            authorization aumodel = new authorization { agency_id = id, start_time = agmodel.start_time, end_time = agmodel.end_time, valid = 1, time = DateTime.Now };
                            aubll.Add(aumodel);//添加授权记录
                            string bgimg = "upload/lfs_img.jpg";
                            string uid = agmodel.id.ToString();
                            string bid = agmodel.account_id.ToString();
                            string sid = agmodel.level_id.ToString();
                            string uname = agmodel.name;
                            string ssatrdate = ((DateTime)agmodel.start_time).ToString("yyyy/MM/dd");
                            string senddate = ((DateTime)agmodel.end_time).ToString("yyyy/MM/dd");
                            string sidname = agmodel.level_name;
                            //string address = "http://wx.nb12315.com/NFS/NFS.html";
                            string address = agmodel.shop_url;
                            if (agmodel.au_code.Substring(0, 4) != "EDJP"||agmodel.au_code.Length>11)
                            {//不是新的编码规则
                                //赋予新的编码规则
                                au_code = "EDJP";
                                DateTime currentTime = DateTime.Now;
                                au_code += "-" + CleanInput(Guid.NewGuid().ToString("N")).Substring(0, 6);
                                agmodel.au_code = au_code;
                                agbll.UpdateAucode(au_code, agency_id);

                            }
                            else
                            {
                                au_code = agmodel.au_code;
                            }
                            //string prams = "bgimg=" + bgimg + "&uid=" + uid + "&bid=" + bid + "&sid=" + sid + "&uname=" + uname + "&ssatrdate=" + ssatrdate + "&senddate=" + senddate + "&sidname=" + sidname + "&address=" + address + "";
                            string prams = "bgimg=" + bgimg + "&uid=" + uid + "&bid=" + bid + "&sid=" + sid + "&uname=" + uname + "&ssatrdate=" + ssatrdate + "&senddate=" + senddate + "&sidname=" + sidname + "&address=" + address + "&au_code=" + au_code + "";       
                    string url = "http://query.m12315.com/nfsImg.aspx?" + prams;
                            System.Net.WebClient web = new System.Net.WebClient();
                            byte[] bytes = web.DownloadData(url);
                            string str = System.Text.Encoding.GetEncoding("UTF-8").GetString(bytes);
                            if (str == "0")
                            {
                                string img_url = "http://m.nfs.m12315.com/tasking/" + uid + "-" + bid + ".jpeg";
                               flag= agbll.UptSq_img(id, img_url);
                            }

                  
                }
                else
                {
                    flag = false;
                }
                if (flag)
                {
                    return Json("0", JsonRequestBehavior.AllowGet);

                }
                else {
                    return Json("100", JsonRequestBehavior.AllowGet);

                }
            }
            catch (Exception)
            {

                return Json("200", JsonRequestBehavior.AllowGet);
            }

        }
        private static string CleanInput(string strIn)
        {
            return Regex.Replace(strIn, "[^\\d]", "");
        }
        #endregion

        #region 删除数据
        [HttpPost]
        public JsonResult delete()
        {
            try
            {
                AccountBLL bll = new AccountBLL();
                int id = Request["id"] != "" ? int.Parse(Request["id"]) : 0;
                if (bll.com_delelte(id, "t_sq_authorization", "valid", 0))
                {
                    return Json(new { MessageCode = "0" }, JsonRequestBehavior.AllowGet);
                }
                else
                {

                    return Json(new { MessageCode = "100" }, JsonRequestBehavior.AllowGet);
                }

            }
            catch (Exception)
            {

                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);
            }


        }
        #endregion
    }
}
